# -*- coding: utf-8 -*-
# @Time    : 2023/1/3 11:18 下午
# @Author  : zuokuijun
# @Email   : zuokuijun13@163.com
import os
import shutil
import numpy
import numpy as np
from scipy import interpolate
from matplotlib import pyplot as plt
import os
import glob
from scipy.interpolate import splev, splprep, interp1d
from scipy.integrate import cumtrapz

"""
第一步：从原始翼型坐标数据集中将闭合的翼型坐标数据提取出来
（只提取起始坐标以及终点坐标为【1，0】的翼型坐标数据）
"""
def get_close_airfoil_coordiante():
    path = ".//data//coord_seligFmt//"
    files = os.listdir(path)
    for i in range(len(files)): ## 依次遍历翼型文件，将闭合的文件挑选出来
        file_path = os.path.join(path, files[i])
        print(files[i])
        with open(file_path, mode="r") as f:
              lines = f.readlines()
              firstLine = lines[1]
              lastLine = lines[-1] # 找到当前文件的最后一行
              first_data = firstLine.split()
              last_data = lastLine.split()
              if (float(first_data[0]) == 1)&(float(first_data[1]) == 0) & (float(last_data[0]) == 1) & (float(last_data[1]) == 0): # 判断最后一行的最后一个数值是否为零
                 shutil.move(file_path, ".//data//initial_data//")
        f.close()

"""
第二步：剔除翼型坐标数据中的第一行字符串，并生成翼型坐标数据（此时的坐标个数并不统一）
"""
def get_xy_database():
    path = "./data/initial_data/"  ## 初始翼型数据库所在绝对路径
    files = os.listdir(path)
    for i in range(len(files)):
        print("当前正在处理的翼型为:{}".format(files[i]))
        file_path = os.path.join(path, files[i])  # 获取翼型的绝对路径
        X = []  # 定义翼型数组，用于保存翼型坐标数据
        Y = []
        with open(file_path, "r") as f:
            lines = f.readlines()
            n = 1
            while n < len(lines):
                line = lines[n].strip("\n")  # 去掉当前行的换行符
                data_base = line.split()  # 根据空格将X、Y坐标分离出来
                X.append(float(data_base[0]))  ## 获取当前翼型的X以及Y坐标
                Y.append(float(data_base[1]))
                n+=1
        xy_coordinate = open("./data/X_Y/{}".format(files[i]), mode="w", encoding="utf8")
        for i in range(len(X)):
            line = str(X[i]) + " " + str(Y[i]) + "\n"
            xy_coordinate.writelines(line)
            xy_coordinate.flush()
        xy_coordinate.close()

"""
第三步：生成pointwise格式的数据
"""
def  get_point_wise_data():
     path="./data/X_Y/"
     files = os.listdir(path)
     for i in range(len(files)):
         X = []
         Y = []
         file_path = os.path.join(path, files[i])  # 翼型文件的绝对路径
         result = open("./data/point_wise_database/{}".format(files[i]), "w", encoding="utf8")
         with open(file_path, mode="r") as f:
            for line in f.readlines():
                line = line.strip("\n")
                lines = line.split()
                X.append(lines[0])
                Y.append(lines[1])
         result.writelines(str(len(X))+ "\n")
         result.flush()
         for i  in range(len(X)):
             temp = X[i] + " " +Y[i] + " " + "0" + "\n"
             result.writelines(temp)
             result.flush()
         result.close()









if __name__ == '__main__':
    # get_close_airfoil_coordiante() # 第一步：从UIUC原始翼型库中寻找闭合的翼型数据
    # get_xy_database() # 第二步：将闭合的翼型数据的第一条字符串剔除，得到纯正的翼型坐标数据
    get_point_wise_data()  # 第三步：获取pointwise数据

